<template>
    <view style="padding: 10px;">
        <view>文件上传</view>
        <u-button text="上传" @click="uploadFile"></u-button>
        <image :src="image_path"></image>
    </view>
</template>

<script>
export default {
  data() {
    return {
      image_path: ''
    }
  },
  methods: {
    chooseImage() {
      plus.gallery.pick(
          (path) => this.uploadFile(path),
          (err) => console.log("取消选择图片"),
          {filter: "image"});
    },
    uploadFile(path) {
      uni.showLoading({title: "上传中..."})
      let task = plus.uploader.createUpload("https://travel.enn.cn/ssop/api/cms-v2/file/upload",
          {method: "POST", priority: 100},
          (t, status) => {
            uni.hideLoading()
            // 上传完成
            if (status == 200) {
              uni.showToast({title: "上传成功！"})
              console.log("Upload success: " + JSON.stringify(t))
              let {responseText} = t
              let {data} = JSON.parse(responseText)
              console.log("responseText", responseText, data)
              this.image_path = data
            } else {
              console.log("Upload failed: " + status)
              uni.showToast({title: "上传失败！"})
            }
          }
      );
      task.addFile(path, {key: "multipartFile"});
      task.addData("string_key", "string_value");
      task.start();
    }
  }
}
</script>

<style>

</style>
